<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>localStorage</title>
</head>
<body>
    <h2>localStorage</h2>
    <button onclick="savaData()">点我保存一个数据</button>
    <button onclick="getData()">点我读取一个数据</button>
    <button onclick="deleteData()">点我删除一个数据</button>
    <button onclick="clearData()">点我清空数据</button>
    <script>
        let p = {
            name: 'haha',
            age: 19
        }
        function savaData() {
            // 只要第二个值传的不是string类型 就会调用toString()方法
            // window.localStorage.setItem('msg', 'hello')
            localStorage.setItem('msg', 'hello')//由于在window上可以直接使用
            // localStorage.setItem('person', p)//只要第二个值传的不是string类型 就会调用toString()方法 会显示为[object Object]
            localStorage.setItem('person', JSON.stringify(p))
            localStorage.setItem('num', 666)
        }
        function getData() {
            console.log(localStorage.getItem('msg'))
            console.log(localStorage.getItem('num'))
            const person = localStorage.getItem('person')
            console.log(JSON.parse(person))//JSON.parse(person)将person转化成对象
            console.log(localStorage.getItem('hahha'))//没有的键值对拿到的为null
        }
        function deleteData() {
            localStorage.removeItem('num')
        }
        function clearData() {
            localStorage.clear()
        }
    </script>
</body>
</html>